package org.wyz.mapreduce.kmeans;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class KMeansReducer extends Reducer<Text, PointWritable, Text, Text> {


    @Override
    protected void reduce(Text key, Iterable<PointWritable> values, Reducer<Text, PointWritable, Text, Text>.Context context) throws IOException, InterruptedException {
        double sumX = 0;
        double sumY = 0;
        int numPoints = 0;
        for (PointWritable point : values) {
            numPoints++;
            sumX += point.getX();
            sumY += point.getY();
        }

        double avgX = sumX / numPoints;
        double avgY = sumY / numPoints;

        context.write(key, new Text(avgX + "," + avgY));
    }
}
